package com.example.sort;

import java.util.Arrays;

/**
 * @author zhanpengguo
 * @date 2019-07-31 15:39
 */
public class BingGui {

    public static Integer[] sort(Integer[] arrays) {
        Integer[] ints = Arrays.copyOf(arrays, arrays.length);
        Integer[] fen = fen(ints);
        return fen;
    }

    private static Integer[] fen(Integer[] arrays) {
        if (arrays.length < 2) {
            return arrays;
        }
        double floor = Math.floor(arrays.length / 2);
        int middle = Double.valueOf(floor).intValue();
        Integer[] left = Arrays.copyOfRange(arrays, 0, middle);
        Integer[] right = Arrays.copyOfRange(arrays, middle, arrays.length);
        return he1(fen(left), fen(right));
    }

    private static Integer[] he(Integer[] left, Integer[] right) {
        int len = left.length + right.length;
        Integer[] he = new Integer[len];
        int l = 0;
        int r = 0;
        for (int i = 0; i < len; i++) {
            if (l > left.length-1) {
                if (r <= right.length-1) {
                    he[i] = right[r];
                    r++;
                }else {
                    return he;
                }
            }else {
                if (r <= right.length-1) {
                    if (left[l] < right[r]) {
                        he[i] = left[l];
                        l++;
                    }else {
                        he[i] = right[r];
                        r++;
                    }
                }else {
                    he[i] = left[l];
                    l++;
                }
            }
        }
        return he;
    }

    private static Integer[] he1(Integer[] left, Integer[] right) {
        int len = left.length + right.length;
        Integer[] he = new Integer[len];
        for (int i = 0, l = 0, r = 0; i < len; i++) {
            if (l >= left.length) {
                he[i] = right[r++];
            } else if (r >= right.length) {
                he[i] = left[l++];
            } else if (left[l] >= right[r]) {
                he[i] = right[r++];
            } else {
                he[i] = left[l++];
            }
        }
        return he;
    }

    public static void main(String[] args) {
        Integer[] integers = {1,2};
        int i=0;
        System.out.println(integers[i++]);
        System.out.println(i);
    }
}
